package com.liy.framework.web.rs.exception;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Provider
public class Jaxrs404ExceptionMapper implements ExceptionMapper<NotFoundException> {
    protected static Logger log = LoggerFactory.getLogger(Jaxrs404ExceptionMapper.class);

    public Jaxrs404ExceptionMapper() {}

    public Response toResponse(NotFoundException exception) {
        log.error(exception.getMessage(), exception);
        ResponseBuilder responseBuilder = Response.status(Status.NOT_FOUND).entity("找不到您请求的资源！");
        responseBuilder.type("text/plain");
        return responseBuilder.encoding("UTF-8").build();
    }
}
